﻿using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response.Tables;
using MinimalistFoodStreet.BackEnd.Infrastructure.EFCore;

namespace MinimalistFoodStreet.BackEnd.Server.Server.ResponseIqExtend
{
    public static class ResponseGoodDetailIqExtend
    {
        public static IQueryable<ResponseGoodsDto> ToResponseGoodDetailIq(this IQueryable<Good> iq)
        {
            return iq.Select(d => new ResponseGoodsDto
            {
                id = d.GId,
                supplierId = d.GSupplierId,
                goodTypeId = d.GGoodTypeId,
                name = d.GName,
                price = d.GPrice,
                description = d.GDescription,
                photo = d.GPhoto,
                detailImages = d.GDetailImages,
                specs = d.GSpecs,
                isDirectly = d.GIsDirectly,
                isShelve = d.GIsShelve,
                inventory = d.GInventory,
                birthday = d.GBirthday,
                expiryday = d.GExpiryday,
                isPromote = d.Promotions != null ? 1 : 0,
                topLevel = d.GTopLevel,
                createTime = d.GCreateTime,
                updateTime = d.GUpdateTime,
                rate = d.Comments.Any() ? d.Comments.Average(d => d.CRate) : null,
                promotePrice = d.Promotions != null ? d.Promotions.Where(d => d.PEndTime > DateTime.Now).First().PDiscountPrice : null,
                supplierShopName = d.GSupplier != null ? d.GSupplier.SShopName : null,
                goodTypeName = d.GGoodType != null ? d.GGoodType.DValue : null
            });
        }
    }
}
